﻿using Microsoft.AspNetCore.Mvc;
using tools.algorithm;

namespace tools.api.Controllers
{
    /// <summary>
    /// SHA-256 工具操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Sha256Controller : ControllerBase
    {
        [HttpGet]
        public IActionResult Compute_String(string text)
        { 
            return Ok(Sha256Tool.GetHash(text));
        }

        [HttpGet]
        public IActionResult Verify_String(string text, string hash)
        {
            return Ok(Sha256Tool.VerifyHash(hash, text));
        }

        [HttpPost]
        public async Task<IActionResult> Compute_File(IFormFile formFile)
        { 
            await using var stream = formFile.OpenReadStream();
            return Ok(Sha256Tool.GetHash(stream));
        }

        [HttpPost]
        public async Task<IActionResult> Verify_File(IFormFile formFile, string hash)
        {
            await using var stream = formFile.OpenReadStream();
            return Ok(Sha256Tool.VerifyHash(hash, stream));
        }
    }
}
